package com.abaaba.project2.controller;

import com.abaaba.project2.bean.Brand;
import com.abaaba.project2.bean.bo.BrandCreateBo;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.bean.vo.BrandCreateVo;
import com.abaaba.project2.service.AdminBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("/admin/brand")
public class AdminBrandController {

    @Autowired
    AdminBrandService adminBrandService;

    @RequestMapping("/list")
    public BaseRespVo list(Integer page,Integer limit,String sort,String order,Integer id,String name) {
        BaseData<Brand> brandBaseData = adminBrandService.selectBrandBySort(page, limit, sort, order,id,name);
        return BaseRespVo.ok(brandBaseData);
    }

    @RequestMapping("/create")
    public BaseRespVo create(@RequestBody  BrandCreateBo brandCreateBo) {




        BrandCreateVo brandCreateVo = null;
        try {

            brandCreateVo = adminBrandService.insertBrand(brandCreateBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(brandCreateVo);
    }

    @RequestMapping("/delete")
    public BaseRespVo delete(@RequestBody Brand brand) {
        try {
            adminBrandService.deleteBrand(brand);
        }catch (Exception e){
            e.printStackTrace();
            return BaseRespVo.fail();
        }

        return BaseRespVo.ok();
    }

    @RequestMapping("/update")
    public BaseRespVo update(@RequestBody Brand brand) {
        try {
            adminBrandService.updateBrand(brand);
        }catch (Exception e){
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }
}
